package com.minglead.springboot.platform.sys.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.minglead.springboot.common.utils.CacheUtils;
import com.minglead.springboot.common.utils.SpringContextHolder;
import com.minglead.springboot.platform.sys.dao.AreaDao;
import com.minglead.springboot.platform.sys.entity.Area;

/**
 * 行政区划工具类
 **/
public class AreaUtils {
	private static AreaDao areaDao=SpringContextHolder.getBean(AreaDao.class);
	private static final String CACHE_AREA_LIST="areaList";
	
	public static String getAreaName(String value,String code,String defaultValue){
		if (StringUtils.isNotBlank(code) && StringUtils.isNotBlank(value)){
			for (Area area : getArea(code)){
				if (value.equals(area.getId())){
					return area.getName();
				}
			}
		}
		return defaultValue;
	}
	
	@SuppressWarnings("unchecked")
	public static String getAreaCode(String Name,String defaultValue){
		List<Area> areaList=(List<Area>)CacheUtils.get(CACHE_AREA_LIST);
		if(areaList==null){
			areaList=areaDao.findAllList(new Area());
			CacheUtils.put(CACHE_AREA_LIST, areaList);
		}
		if (StringUtils.isNotBlank(Name)){
			for(Area area :areaList){
				if (Name.equals(area.getName())){
					return area.getCode();
				}
			}
		}
		return defaultValue;
	}
	@SuppressWarnings("unchecked")
	public static List<Area> getArea(String code){
		List<Area> areaList=(List<Area>)CacheUtils.get(CACHE_AREA_LIST);
		if(areaList==null){
			areaList=areaDao.getSubListByCode(code);
			CacheUtils.put(code, areaList);
		}
		return areaList;
	}
}
